home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50b Issue 142 (CD142b) (August 1998).iso / essent / FIXES / CSeries.exe / issue100 / LIFELITE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-01  |  4.0 KB  |  209 lines

  1. /*              LIFELITE.CPP BY IAN SHARPE
  2.  
  3.                ****************************
  4.                  LIFE LITE PRIMER EDITION
  5.                ****************************
  6.  
  7.                Comments in file LIFELITE.TXT
  8.  
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <ctype.h>
  14. #include <conio.h>
  15. #include <dos.h>
  16.  
  17. ////////////////////////////
  18. #define         HSIZE 40
  19. #define         VSIZE 24
  20.  
  21. #define    KEEP_GOING 1
  22. #define          QUIT 0
  23.  
  24. #define          DEAD 0
  25. #define          LIVE 1
  26.  
  27. #define     CURSOR_UP 72
  28. #define   CURSOR_DOWN 80
  29. #define   CURSOR_LEFT 75
  30. #define  CURSOR_RIGHT 77
  31.  
  32. ////////////////////////////
  33. int world[VSIZE+2][HSIZE+2][2];
  34.  
  35. ////////////////////////////
  36. void tidyup(void)
  37. {
  38.     textcolor(WHITE);
  39.     textbackground(BLACK);
  40.     clrscr();
  41.     _setcursortype(_NORMALCURSOR);
  42. }
  43.  
  44. ////////////////////////////
  45. void showcell(int worldrow, int worldcol, int state)
  46. {
  47.     gotoxy( (worldcol*2)-1, worldrow);
  48.     textcolor(RED);
  49.     textbackground(WHITE);
  50.     cputs( (state == LIVE) ? "██" : "  " );
  51. }
  52.  
  53. ////////////////////////////
  54. void movecursor(int fromx, int fromy, int tox, int toy)
  55. {
  56.     showcell(fromy, fromx, world[fromy][fromx][0]);
  57.     gotoxy( (tox*2)-1, toy);
  58.     textcolor( (world[toy][tox][0]==LIVE) ? WHITE : BLACK );
  59.     textbackground( (world[toy][tox][0]==LIVE) ? RED : WHITE );
  60.     cputs("≡≡");
  61. }
  62.  
  63. ////////////////////////////
  64. int input_seed(void)
  65. {
  66. int x, y, nx, ny;
  67.  
  68.     for(y=0; y<=VSIZE+1; y++)
  69.         for(x=0; x<=HSIZE+1; x++)
  70.             world[y][x][0]=world[y][x][1]=DEAD;
  71.  
  72.     _setcursortype(_NOCURSOR);
  73.     textbackground(WHITE);
  74.     clrscr();
  75.  
  76.     textcolor(YELLOW);
  77.     textbackground(GREEN);
  78.     gotoxy(1,25);
  79.     clreol();
  80.     printf(" [Arrow keys]=move round * [Spacebar]=toggle cell * [Return]=breed * [Q]=quit");
  81.  
  82.     textcolor(RED);
  83.     textbackground(WHITE);
  84.     x=HSIZE/2;
  85.     y=VSIZE/2;
  86.     movecursor(x, y, x, y);
  87.  
  88.     while( 1 )
  89.         switch( toupper(getch()) )
  90.             {
  91.             case '\r':
  92.                 return(KEEP_GOING);
  93.             case 'Q':
  94.                 return(QUIT);
  95.             case ' ':
  96.                 world[y][x][0] ^= LIVE;
  97.                 showcell(y, x, world[y][x][0]);
  98.                 movecursor(x, y, x, y);
  99.                 break;
  100.             case '\0':
  101.                 nx=x; ny=y;
  102.                 switch( getch() )
  103.                     {
  104.                     case CURSOR_UP:
  105.                         ny--;
  106.                         break;
  107.                     case CURSOR_DOWN:
  108.                         ny++;
  109.                         break;
  110.                     case CURSOR_LEFT:
  111.                         nx--;
  112.                         break;
  113.                     case CURSOR_RIGHT:
  114.                         nx++;
  115.                     }
  116.                 nx=( nx==0 || nx==HSIZE+1 ) ? x : nx;
  117.                 ny=( ny==0 || ny==VSIZE+1 ) ? y : ny;
  118.                 movecursor(x, y, nx, ny);
  119.                 x=nx; y=ny;
  120.             }
  121. }
  122.  
  123. ////////////////////////////
  124. int breed(void)
  125. {
  126. int x, y, oldgen, newgen, sum, healthy;
  127.  
  128.     gotoxy(1, 25);
  129.     textcolor(YELLOW);
  130.     textbackground(BLUE);
  131.     clreol();
  132.     gotoxy(23, 25);
  133.     printf("[Return]=edit new seed * [Q]=quit");
  134.  
  135.     textcolor(RED);
  136.     textbackground(WHITE);
  137.  
  138.     oldgen=0; newgen=1; healthy=1;
  139.     while( 1 )
  140.         {
  141.         while( kbhit() == 0 )
  142.             {
  143.             if( ! healthy )
  144.                 {
  145.                 gotoxy(30,12);
  146.                 cputs("They're dead, Jim");
  147.                 continue;
  148.                 }
  149.  
  150.             healthy=0;
  151.             for(y=1; y<=VSIZE; y++)
  152.                 for(x=1; x<=HSIZE; x++)
  153.                     {
  154.                     sum=world[y-1][x-1][oldgen]
  155.                        +world[y-1][x][oldgen]
  156.                        +world[y-1][x+1][oldgen]
  157.                        +world[y][x-1][oldgen]
  158.                        +world[y][x+1][oldgen]
  159.                        +world[y+1][x-1][oldgen]
  160.                        +world[y+1][x][oldgen]
  161.                        +world[y+1][x+1][oldgen];
  162.  
  163.                     switch( sum )
  164.                         {
  165.                         case 2:
  166.                             world[y][x][newgen]= ( world[y][x][oldgen] == LIVE ) ? LIVE : DEAD;
  167.                             break;
  168.                         case 3:
  169.                             world[y][x][newgen]=LIVE;
  170.                             break;
  171.                         default:
  172.                             world[y][x][newgen]=DEAD;
  173.                         }
  174.                     showcell(y, x, world[y][x][newgen]);
  175.                     healthy+=sum;
  176.                     }
  177.             oldgen ^= 1; newgen ^= 1;
  178.             }
  179.  
  180.         switch( toupper(getch()) )
  181.             {
  182.             case '\r':
  183.                 return(KEEP_GOING);
  184.             case 'Q':
  185.                 return(QUIT);
  186.             }
  187.         }
  188. }
  189.  
  190. ////////////////////////////
  191. void main(void)
  192. {
  193. int action;
  194.  
  195.     action=KEEP_GOING;
  196.     while ( action == KEEP_GOING )
  197.         {
  198.         if( (action=input_seed()) == QUIT )
  199.             continue;
  200.         action=breed();
  201.         }
  202.  
  203.     tidyup();
  204. }
  205.  
  206.     ////////////////////////
  207.     // THAT'S ALL, FOLKS! //
  208.     ////////////////////////
  209.